package gui;

import gui.kat.Kategori;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * Author: Kostika Gorica
 * User: kostika
 * Date: 1/21/13
 * Time: 10:02 AM
 */
public class PaneliLojes extends JPanel implements ActionListener{
    public static final int NO_ROUNDEVE = 13;
    public static final int KUFIRI_BONUS = 63;
    public static final int PIKET_BONUS = 35;
    public JLabel[][] piketLabels;

    private int lojtariRradhes=0;
    private int numriLojtareve=4;
    private Lojtari lojtaret[];

    ZariPanel zariPanel;

    public PaneliLojes(){
        super();
        setSize(200,400);
        setLayout(new GridBagLayout());
        setBorder(BorderFactory.createTitledBorder("Yahtzeee! Game!"));

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;

        c.insets = new Insets(5,20,5,20);
        c.anchor = GridBagConstraints.CENTER;

        c.gridx = 0;
        c.gridy = 0;
        add(new JLabel("Lojtaret:"),c);

        try{
            numriLojtareve=Integer.parseInt(JOptionPane.showInputDialog("Jepni numrin e Lojtareve:",4));
        }catch (NumberFormatException e){
            numriLojtareve=4;
        }

        lojtaret=new Lojtari[numriLojtareve+1];
        for(int i=1; i<lojtaret.length; i++){
            c.gridx=i;
            String emri=JOptionPane.showInputDialog("Emri : ",("Player"+i));
            lojtaret[i]=new Lojtari(emri);
            add(lojtaret[i],c);
        }

        c.gridx=0;
        c.gridy = 1;
        c.gridwidth=1;

        for (int i=1; i<=6; i++){
            c.gridy = i;
            add(Kategori.kategorite.get(i), c);
            Kategori.kategorite.get(i).addActionListener(this);
        }

        c.gridy = 7;
        add(new JLabel("Piket e Siperme"),c);
        c.gridy = 8;
        add(new JLabel("Bonus"),c);

        for (int i=9; i<=15; i++){
            c.gridy = i;
            add(Kategori.kategorite.get(i), c);
            Kategori.kategorite.get(i).addActionListener(this);
        }

        c.gridy = 16;
        add(new JLabel("Piket e Poshtme"),c);
        c.gridy = 17;
        add(new JLabel("TOTAL"),c);


        c.gridx = 0;
        c.gridy = 0;

        piketLabels = new JLabel[numriLojtareve+1][18];
        for (int j=1;j<piketLabels.length; j++){
            c.gridx=j;
            for(int i=1; i<18; i++){
                c.gridy=i;
                piketLabels[j][i]=new JLabel(""+lojtaret[j].getPiketKategori(i));
                add(piketLabels[j][i], c);
            }
        }

        c.gridx = 6;
        c.gridy = 1;
        c.gridheight = 18;
        zariPanel=new ZariPanel();
        add(zariPanel,c);

        lojtariTjeter();
    }

    public void actionPerformed(ActionEvent e) {
        if(zariPanel.getRoundi()<= NO_ROUNDEVE){
            Kategori k = (Kategori) e.getSource();
            int pike = k.llogaritPiket(zariPanel.getKombinimizarave());
            lojtaret[lojtariRradhes].addPike(k.getIndex(),pike);
            piketLabels[lojtariRradhes][k.getIndex()].setText(""+lojtaret[lojtariRradhes].getPiketKategori(k.getIndex()));
            zariPanel.resetZarat();


            // bej update labelat e pikeve siperme/poshtme/bonus/total
            int piketSiperme=lojtaret[lojtariRradhes].getPiketSiperme();
            int pikeBonus=0;
            if(piketSiperme> KUFIRI_BONUS){
                pikeBonus=PIKET_BONUS;
                piketLabels[lojtariRradhes][Kategori.BONUS].setText("" + PIKET_BONUS);
            }
            piketLabels[lojtariRradhes][Kategori.PIKET_SIPERME].setText("" + piketSiperme);
            int piketPoshtme=lojtaret[lojtariRradhes].getPiketPoshtme();
            piketLabels[lojtariRradhes][Kategori.PIKET_POSHTME].setText("" + piketPoshtme);
            int totaliPikeve=piketSiperme+piketPoshtme+pikeBonus;
            piketLabels[lojtariRradhes][Kategori.TOTALI].setText("" + totaliPikeve);


            lojtariTjeter();
        } else {
            for (int i=1; i<=numriLojtareve; i++){
                int piketSiperme=lojtaret[i].getPiketSiperme();
                if(piketSiperme> KUFIRI_BONUS){
                    piketLabels[i][Kategori.BONUS].setText(""+ PIKET_BONUS);
                    lojtaret[i].addPike(8,35);
                }
                piketLabels[i][Kategori.PIKET_SIPERME].setText(""+piketSiperme);
                lojtaret[i].addPike(Kategori.PIKET_SIPERME,piketSiperme);
                int piketPoshtme=lojtaret[i].getPiketPoshtme();
                piketLabels[i][Kategori.PIKET_POSHTME].setText(""+piketPoshtme);
                lojtaret[i].addPike(Kategori.PIKET_POSHTME,piketPoshtme);
                int totaliPikeve=lojtaret[i].getTotaliPikeve();
                piketLabels[i][Kategori.TOTALI].setText(""+totaliPikeve);
                lojtaret[i].addPike(Kategori.TOTALI,totaliPikeve);
            }
            Lojtari fitues=lojtariFitues();
            JOptionPane.showMessageDialog(this,"Lojtari Fitues: "+
                    fitues.getEmri()+"\nPike: "+fitues.getTotaliPikeve());
        }

    }

    private void lojtariTjeter(){
        lojtariRradhes=lojtariRradhes+1;
        if(lojtariRradhes>numriLojtareve){
            zariPanel.roundiTjeter();
            lojtariRradhes=1;
        }
        for (int i=1; i<lojtaret.length; i++){
            lojtaret[i].setBackground(Color.cyan);
        }
        lojtaret[lojtariRradhes].setBackground(Color.GREEN);

    }

    public Lojtari lojtariFitues(){
        int index = 1;
        int pike=lojtaret[1].getTotaliPikeve();
        for(int i=1; i<numriLojtareve-1; i++){
            if (lojtaret[i].getTotaliPikeve()>pike){
                index=i;
                pike=lojtaret[i].getTotaliPikeve();
            }
        }
        return lojtaret[index];
    }
}
